<template>
  <div class="main">
    <div class="title">
      聊天室（{{name}}）
      <div
        class="logout"
        @click="handleLogout">
        注销
      </div>
    </div>
    <div id="msgs" class="msgs">
      <div
        class="detail"
        v-for="item in messages"
        :key="item.id">
        <div class="time">{{tran(item.time)}}</div>
        <div :class="item.username === name ? 'msg-self' : 'msg'">
          <div class="head">
            <img
              class="headimg"
              :src="item.userimgUrl ? item.userimgUrl : 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1556445229433&di=23e4c9de1148e20b210435c8a6624b1c&imgtype=0&src=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201312%2F05%2F20131205172433_GPhxi.thumb.600_0.jpeg'">
          </div>
          <div :class="item.username === name ? 'nickname-self' : 'nickname'">
            <div class="name">
              {{item.username}}
            </div>
            <div class="message" :class="item.username === name ? 'message-self' : ''">
              {{item.message}}
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="send">
      <textarea
        class="content"
        placeholder="请输入发送消息"
        v-model="content">
      </textarea>
      <div class="btn">
        <el-button @click="sendMsg">
          发送（S）
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {apiAuth} from '@/properties/api.js'
import {mapState} from 'vuex'
export default {
  name: 'Chat',
  props: {
    name: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      content: ''
    }
  },
  computed: {
    ...mapState(['ws', 'user', 'messages'])
  },
  mounted () {
    this.$nextTick(function () {
      let div = document.getElementById('msgs')
      div.scrollTop = div.scrollHeight
    })
  },
  methods: {
    tran (time) {
      return this.$utils.transformTime(time)
    },
    logout (cbOk, cbErr) {
      this.$axios.post(
        apiAuth.logout,
        null,
        cbOk,
        cbErr
      )
    },

    handleLogout () {
      let $this = this
      $this.logout(
        function (res) {
          $this.$message.success(res.msg)
          $this.ws.close()
          $this.$router.push({name: 'Login'})
        }, function (err) {
          $this.$message.error(err.msg)
          $this.$router.push({name: 'Login'})
        }
      )
    },

    sendMsg () {
      this.ws.send(this.content)
      this.content = ''
    }
  },

  watch: {
    messages: function () {
      this.$nextTick(function () {
        let div = document.getElementById('msgs')
        div.scrollTop = div.scrollHeight
      })
    }
  }
}
</script>

<style scoped>
.main {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.title {
  box-sizing: border-box;
  padding-left: 5px;
  flex: 0 0 50px;
  line-height: 50px;
  font-size: 30px;
  text-align: left;
  width: 100%;
  background-color: #ddd;
  position: relative;
}

.logout {
  position: absolute;
  top: 0;
  right: 10px;
  color: blue;
  font-size: 20px;
  text-decoration: underline;
  cursor: pointer;
}

.msgs {
  flex: 1;
  overflow-y: auto;
}

.detail {
  margin-bottom: 5px;
}

.time {
  margin: 0 auto;
  text-align: center;
  width: 200px;
  background-color: #bbb;
  color: #eee;
  border-radius: 5px;
}

.msg {
  display: flex;
}

.msg-self {
  display: flex;
  flex-direction: row-reverse;
}

.head {
  flex: 0 0 80px;
  height: 80px;
}

.headimg {
  margin: 10px;
  width: 60px;
  height: 60px;
}

.nickname {
  box-sizing: border-box;
  flex: 0 0 70%;
  text-align: left;
}

.nickname-self {
  box-sizing: border-box;
  flex: 0 0 70%;
  text-align: right;
}

.name {
  margin: 4px 0 0 0;
  width: 100%;
  height: 20px;
  line-height: 20px;
  color: #666;
}

.message {
  box-sizing: border-box;
  padding: 5px;
  font-size: 20px;
  text-align: left;
  display: inline-block;
  background-color: #ccc;
  border-radius: 5px;
}

.message-self {
  background-color: #6c6;
}

.send {
  flex: 0 0 300px;
  width: 100%;
}

.content {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  width: 100%;
  height: 250px;
  font-size: 25px;
  border: none;
  border-top: 1px solid #ccc;
  padding: 10px;
}

.btn {
  text-align: right;
}
</style>
